package com.gzhryc.shared_device.common.payment.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.base.enums.ERefundState;
import com.gzhryc.shared_device.common.payment.dao.AliRefundOrderDao;
import com.gzhryc.shared_device.common.payment.dao.db.AliRefundOrder;
import com.gzhryc.shared_device.common.payment.dao.db.AlipayOrder;

public class AliRefundOrderService {

	static Logger log = Logger.getLogger(AliRefundOrderService.class);

	public static AliRefundOrderService self(Date date) {
		return MultiDBTools.getService(AliRefundOrderService.class, date);
	}

	public static AliRefundOrderService self(String jdbcKey,Date date) {
		return MultiDBTools.getService(jdbcKey, AliRefundOrderService.class, date);
	}

	AliRefundOrderDao dao;

	public AliRefundOrderService(String jdbcKey,Date date) {
		dao = new AliRefundOrderDao(jdbcKey,date);
	}

	public List<AliRefundOrder> findByPayTradeNo(String payTradeNo){
		if(StringTools.isNotBlank(payTradeNo)){
			Conditions conditions = new Conditions(AliRefundOrder.class);
			conditions.and().is("payTradeNo",payTradeNo);

			try {
				return dao.findByCondition(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return null;
	}

	public Long countByPayTradeNo(String payTradeNo){
		if(StringTools.isNotBlank(payTradeNo)){
			Conditions conditions = new Conditions(AliRefundOrder.class);
			conditions.and().is("payTradeNo",payTradeNo);

			try {
				return dao.count(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return null;
	}

	public List<AliRefundOrder> findByBusinessTradeNo(String businessTradeNo){
		if(StringTools.isNotBlank(businessTradeNo)){
			Conditions conditions = new Conditions(AliRefundOrder.class);
			conditions.and().is("businessTradeNo",businessTradeNo);

			try {
				return dao.findByCondition(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return null;
	}

	public AliRefundOrder getByTradeNo(String tradeNo){
		try {
			return dao.getById(tradeNo);
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return null;
	}

	public AliRefundOrder createRefundOrder(AlipayOrder payOrder, Integer refundMoney, String note) throws LogicException {
		Long total = countByPayTradeNo(payOrder.getTradeNo());
		if(total == null){total = 0L;}
		String tradeNo = payOrder.getTradeNo() + String.format("%02d",total);

		AliRefundOrder refundOrder = new AliRefundOrder();
		refundOrder.setTradeNo(tradeNo);
		refundOrder.setNote(note);
		refundOrder.setPayTradeNo(payOrder.getTradeNo());
		refundOrder.setExternalPayTradeNo(payOrder.getExternalTradeNo());
		refundOrder.setPayMoney(payOrder.getPayMoney());
		refundOrder.setBusinessType(payOrder.getBusinessType());
		refundOrder.setBusinessTradeNo(payOrder.getBusinessTradeNo());
		refundOrder.setAppType(payOrder.getAppType());
		refundOrder.setAppId(payOrder.getAppId());
		refundOrder.setPayerId(payOrder.getPayerId());
		refundOrder.setBusinessAccount(payOrder.getBusinessAccount());
		refundOrder.setTradeType(payOrder.getTradeType());
		refundOrder.setRefundMoney(refundMoney);
		refundOrder.setCreateDate(new Date());

		try {
			if(dao.insert(refundOrder) > 0){
				return refundOrder;
			}
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return null;
	}

	public boolean toFail(String tradeNo, String errorMsg,String externalContent) {
		if (StringTools.isNotBlank(tradeNo)) {
			AliRefundOrder refundOrder = new AliRefundOrder();
			refundOrder.setTradeNo(tradeNo);
			refundOrder.setRefundStatus(ERefundState.Fail.index());
			refundOrder.setFailNote(errorMsg);
			refundOrder.setExternalContent(externalContent);
			refundOrder.setRefundDate(new Date());

			try {
				if (dao.updateNotNull(refundOrder) > 0) {
					return true;
				}
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}

		}
		return false;
	}

	public boolean toSuccess(String tradeNo,String externalTradeNo,String externalContent){
		if (StringTools.isNotBlank(tradeNo)) {
			AliRefundOrder refundOrder = new AliRefundOrder();
			refundOrder.setTradeNo(tradeNo);
			refundOrder.setRefundStatus(ERefundState.Success.index());
			refundOrder.setExternalTradeNo(externalTradeNo);
			refundOrder.setExternalContent(externalContent);
			refundOrder.setRefundDate(new Date());

			try {
				if (dao.updateNotNull(refundOrder) > 0) {
					return true;
				}
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return false;
	}
}
